#ifndef PUBLIC_H
#define PUBLIC_H
#include <QGraphicsView>
#include <QWheelEvent>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include "maingraphicsview.h"


class ZoomGraphicsRectItem : public QGraphicsRectItem {
public:
    ZoomGraphicsRectItem(QGraphicsItem *parent = nullptr) : QGraphicsRectItem(parent) {}
    void setMainView(QGraphicsView *view) { m_mainView = view; }
    QGraphicsView *m_mainView;
    bool isMove() { return m_isMove; }
private:
    bool m_isMove = false;
protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
        // 处理鼠标移动事件
        if (isSelected() && event->buttons() & Qt::LeftButton) {
            m_isMove = true;
            QRectF visibleRectItem = mapToScene(rect()).boundingRect();
            m_mainView->setSceneRect(sceneBoundingRect());
        }
        // 继续传递事件给父类处理
        QGraphicsRectItem::mouseMoveEvent(event);
    }

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
        // 处理鼠标释放事件
        if (event->button() == Qt::LeftButton && isSelected()) {
            m_isMove = false;
        }
        // 继续传递事件给父类处理
        QGraphicsRectItem::mouseReleaseEvent(event);
    }
};
class ZoomScene : public QGraphicsScene {
public:
    ZoomScene(QWidget *parent = nullptr) : QGraphicsScene(parent) {}
    void setPixMap(const QPixmap &pixmap) { m_pixmap = pixmap; }
    void setMainView(QGraphicsView *view) { m_mainView = view; }
    bool isSetPos() { return m_isSetPos; }
protected:
    void drawBackground(QPainter *painter, const QRectF &rect) override {
        painter->drawPixmap(sceneRect().toRect(), m_pixmap);
    }
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override {
        QPointF p = event->scenePos();
        bool b = items().first()->sceneBoundingRect().contains(event->scenePos());
        if (!b) {
            m_isSetPos = true;
            items().first()->setPos(event->scenePos());
            items().first()->update();
            update();
            m_mainView->setSceneRect(items().first()->sceneBoundingRect());
        } else
            m_isSetPos = false;
        QGraphicsScene::mousePressEvent(event);
    }
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
        m_isSetPos = false;
        QGraphicsScene::mouseReleaseEvent(event);
    }

private:
    bool m_isSetPos = false;
    QPixmap m_pixmap;
    QGraphicsView *m_mainView;
};
class ZoomGraphicsView : public QGraphicsView {
public:
    ZoomGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) {}

     protected:
       // 重写wheelEvent，传递事件给另一个QGraphicsView
       void wheelEvent(QWheelEvent *event) override {
           // 调用父类的wheelEvent方法处理当前视图的滚轮事件
           QGraphicsView::wheelEvent(event);

           // 如果有另一个QGraphicsView对象，可以将事件传递给它
           if (otherView) {
               otherView->wheelEvent(event); // 传递给另一个QGraphicsView
           }
       }

    public:
       void setMainView(MainGraphicsView*view) { otherView = view; }

     private:
       MainGraphicsView *otherView = nullptr; // 存储另一个QGraphicsView的指针
};
#endif // PUBLIC_H
